{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "class SimpleLinearRegression2:\n",
    "    def __init__(self):\n",
    "        \"\"\"初始化Single Linear Regression模型\"\"\"\n",
    "        self.a_ = None\n",
    "        self.b_ = None\n",
    "        \n",
    "    def fit(self, x_train, y_train):\n",
    "        \"\"\"根据训练数据集X_train, y_train训练Single Linear Regression模型\"\"\"\n",
    "        assert x_train.ndim == 1, \"Simple Linear Regressor can only solve single feature training data\"\n",
    "        assert len(x_train) == len(y_train), \"the size of x_train must be equal to the size of y_train\"\n",
    "        \n",
    "        x_mean = np.mean(x_train)\n",
    "        y_mean = np.mean(y_train)\n",
    "    \n",
    "        num = (x_train - x_mean).dot(y_train - y_mean)\n",
    "        d = (x_train - x_mean).dot(x_train - x_mean)\n",
    "\n",
    "        self.a_ = num / d\n",
    "        self.b_ = y_mean - self.a_ * x_mean\n",
    "        \n",
    "    def predict(self, x_predict):\n",
    "        \"\"\"给定待测数据集X_predict，返回表示x_predict的结果向量\"\"\"\n",
    "        assert x_predict.ndim == 1, \"Simple Linear Regressor can only solve single feature training data\"\n",
    "        assert self.a_ is not None and self.b_ is not None, \"must fit before predict\"\n",
    "        return [self._predict(x) for x in x_predict]\n",
    "    \n",
    "    def _predict(self, x_single):\n",
    "        \"\"\"给定单个待预测数据s_single，返回x_single的预测结果\"\"\"\n",
    "        return self.a_ * x_single + b\n",
    "    \n",
    "    def __repr__(self):\n",
    "        return \"SimpleLinearRegression2()\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'a' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-4-c60fd104bd20>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      5\u001b[0m \u001b[0mreg2\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mfit\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mx\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0my\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      6\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 7\u001b[1;33m \u001b[0my_hat2\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0ma\u001b[0m \u001b[1;33m*\u001b[0m \u001b[0mx\u001b[0m \u001b[1;33m+\u001b[0m \u001b[0mb\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      8\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      9\u001b[0m \u001b[0mplt\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mscatter\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mx\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0my\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mNameError\u001b[0m: name 'a' is not defined"
     ]
    }
   ],
   "source": [
    "x = np.array([1., 2., 3., 4., 5.])\n",
    "y = np.array([1., 3., 2., 3., 5.])\n",
    "\n",
    "reg2 = SimpleLinearRegression2()\n",
    "reg2.fit(x, y)\n",
    "\n",
    "y_hat2 =reg2.predict(x)\n",
    "\n",
    "plt.scatter(x, y)\n",
    "plt.plot(x, y_hat2, color='r')\n",
    "plt.axis([0, 6, 0, 6])\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
